package com.xhcl.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xhcl.common.annotation.Log;
import com.xhcl.common.core.controller.BaseController;
import com.xhcl.common.core.domain.AjaxResult;
import com.xhcl.common.core.domain.Stree;
import com.xhcl.common.core.page.TableDataInfo;
import com.xhcl.common.enums.BusinessType;
import com.xhcl.common.utils.StringUtils;
import com.xhcl.framework.uitls.poi.ExcelUtil;
import com.xhcl.system.domain.TGgKmdm;
import com.xhcl.system.domain.TSrhsHsxtcs;
import com.xhcl.system.service.ITGgKmdmService;
import com.xhcl.system.service.ITSrhsHsxtcsService;

/**
 * 会计科目Controller
 * 
 * @author xhcl
 * @date 2020-08-25
 */
@Controller
@RequestMapping("/system/kmdm")
public class TGgKmdmController extends BaseController
{
    private String prefix = "system/kmdm";

    @Autowired
    private ITGgKmdmService tGgKmdmService;
    @Autowired
    private ITSrhsHsxtcsService tSrhsHsxtcsService;

    @RequiresPermissions("system:kmdm:view")
    @GetMapping()
    public String kmdm(ModelMap mmap)
    {
    	TSrhsHsxtcs tSrhsHsxtcs=new TSrhsHsxtcs();
    	tSrhsHsxtcs.setCsdm("year");
    	List<TSrhsHsxtcs> ylist=tSrhsHsxtcsService.selectTSrhsHsxtcsList(tSrhsHsxtcs);
    	String year=ylist.stream().findFirst().orElse(new TSrhsHsxtcs()).getCsnr();
    	List<String> ndlist=new ArrayList();
    	for (int i=0;i<5;i++) {
    		ndlist.add(String.valueOf(Integer.parseInt(year)-i));
    	}
    	mmap.put("ndlist", ndlist);
        return prefix + "/kmdm";
    }

    /**
     * 查询会计科目树列表
     */
    @RequiresPermissions("system:kmdm:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TGgKmdm tGgKmdm)
    {
    	startPage();
    	List<TGgKmdm> list = tGgKmdmService.selectTGgKmdmList(tGgKmdm);
      	return getDataTable(list);
    }

    /**
     * 导出会计科目列表
     */
    @RequiresPermissions("system:kmdm:export")
    @Log(title = "会计科目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TGgKmdm tGgKmdm)
    {
        List<TGgKmdm> list = tGgKmdmService.selectTGgKmdmList(tGgKmdm);
        ExcelUtil<TGgKmdm> util = new ExcelUtil<TGgKmdm>(TGgKmdm.class);
        return util.exportExcel(list, "kmdm");
    }

    /**
     * 新增会计科目
     */
    @GetMapping(value = { "/add/{nd}", "/add/" })
    public String add(@PathVariable(value = "kmdm", required = false) String kmdm, ModelMap mmap)
    {
        if (StringUtils.isNotNull(kmdm))
        {
            mmap.put("tGgKmdm", tGgKmdmService.selectTGgKmdmById(kmdm));
        }
        TSrhsHsxtcs tSrhsHsxtcs=new TSrhsHsxtcs();
    	tSrhsHsxtcs.setCsdm("year");
    	List<TSrhsHsxtcs> ylist=tSrhsHsxtcsService.selectTSrhsHsxtcsList(tSrhsHsxtcs);
    	String year=ylist.stream().findFirst().orElse(new TSrhsHsxtcs()).getCsnr();
    	List<String> ndlist=new ArrayList();
    	for (int i=0;i<5;i++) {
    		ndlist.add(String.valueOf(Integer.parseInt(year)-i));
    	}
    	mmap.put("ndlist", ndlist);
        return prefix + "/add";
    }

    /**
     * 新增保存会计科目
     */
    @RequiresPermissions("system:kmdm:add")
    @Log(title = "会计科目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TGgKmdm tGgKmdm)
    {  
    	
        return toAjax(tGgKmdmService.insertTGgKmdm(tGgKmdm));
    }

    /**
     * 修改会计科目
     */
    @GetMapping("/edit/{kmdm}")
    public String edit(@PathVariable("kmdm") String kmdm, ModelMap mmap)
    {
        TGgKmdm tGgKmdm = tGgKmdmService.selectTGgKmdmById(kmdm);
        mmap.put("tGgKmdm", tGgKmdm);
        return prefix + "/edit";
    }

    /**
     * 修改保存会计科目
     */
    @RequiresPermissions("system:kmdm:edit")
    @Log(title = "会计科目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TGgKmdm tGgKmdm)
    {
        return toAjax(tGgKmdmService.updateTGgKmdm(tGgKmdm));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:kmdm:remove")
    @Log(title = "会计科目", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{kmdm}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("kmdm") String kmdm)
    {
        return toAjax(tGgKmdmService.deleteTGgKmdmById(kmdm));
    }

    /**
     * 选择会计科目树
     */
    @GetMapping(value = { "/selectKmdmTree/{kmdm}", "/selectKmdmTree/" })
    public String selectKmdmTree(@PathVariable(value = "kmdm", required = false) String kmdm, ModelMap mmap)
    {
        if (StringUtils.isNotNull(kmdm))
        {
            mmap.put("tGgKmdm", tGgKmdmService.selectTGgKmdmById(kmdm));
        }
        return prefix + "/tree";
    }

    /**
     * 加载会计科目树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Stree> treeData()
    {   
    	
        List<Stree> ztrees = tGgKmdmService.selectTGgKmdmTree();
        return ztrees;
    }
    
    /**
     * 加载会计科目树列表
     */
    @GetMapping("/listtreeData/{nd}")
    @ResponseBody
    public List<Stree> listtreeData(@PathVariable(value = "nd", required = false) Long nd)
    {
        List<Stree> ztrees = tGgKmdmService.selectTGgKmdmTree(nd);
        return ztrees;
    }
}
